package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.mainTopFrame;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JTable;

import org.HisCanvas.ChrisRiffle.JFlightLog.common.LogBookRecUtils;
import org.HisCanvas.ChrisRiffle.JFlightLog.dataModel.LogBookRec;
import org.HisCanvas.ChrisRiffle.JFlightLog.launching.JFlightLogController;
import org.HisCanvas.ChrisRiffle.JFlightLog.logging.LogController;
import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.mainFrameMenu.MainFrameMenuController;
import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.mainFrameToolBar.MainFrameToolBarController;
import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table.FlightLogTableModel;
import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table.FlightLogTableModelSetterWorker;



/**
 * FlightLogMainFrameController
 * 
 * @author RIF 
 */
public class FlightLogMainFrameController {
	
	
	private FlightLogMainFrame flightLogMainFrame;
	
	
//	private static final Dimension MAIN_FRAME_SIZE = new Dimension(1200,400);
	
	private static FlightLogMainFrameController INSTANCE;
	
	
	private FlightLogMainFrameController() {	
	}
	
	
	/**
	 * singleton 
	 * 
	 * @return
	 * 		singleton
	 */
	public static FlightLogMainFrameController instance(){
		
		if(INSTANCE == null){
			
			INSTANCE = new FlightLogMainFrameController();
		}
		return INSTANCE;
	}
	
	
	
	
	/**
	 * getFlightLogMainFrame
	 * @return
	 */
	private FlightLogMainFrame getFlightLogMainFrame(){
		
		if(flightLogMainFrame == null){
			
			flightLogMainFrame = new FlightLogMainFrame();
			
//			flightLogMainFrame.setPreferredSize(MAIN_FRAME_SIZE);
			
			configMainFrame();
		}
		return flightLogMainFrame;				
	}
	

	/**
	 * configMainFrame
	 * @param pMainFrame
	 */
	private void configMainFrame(){
		
		
		flightLogMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		flightLogMainFrame.addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
						
				try{
					JFlightLogController.instance().getLogBookDao().close();
					
				}catch (Exception ex) {
					
					ex.printStackTrace();
					LogController.severe("jflog errors when closing: " + ex.getMessage());
				}				
				System.exit(0);			
			}			
		});
		
		//this will should set edit buttons/menuItems disabled b/c there shouldn't be anything 
		// selected in the table for editing
		configureItemsRequiringTableSelection();
		
		flightLogMainFrame.pack();
		flightLogMainFrame.setLocationRelativeTo(null);//centers
	}
	
	
	/**
	 * displayMainFrame
	 */
	public void displayMainFrame(){
		
		FlightLogMainFrame frame = getFlightLogMainFrame();		
		
		frame.setVisible(true);			
	}


	public void configureItemsRequiringTableSelection() {

		MainFrameMenuController.instance().configureItemsRequiringTableSelection();
		MainFrameToolBarController.instance().configureItemsRequiringTableSelection();
		
	}
	
	
}









